Repository navigation
Avoid integer overflow in find_peaks - #664
Conversation
Compare adjacent integer samples directly, keeping the existing floating-point path unchanged.
Cover signed and unsigned dtype limits, plateaus, uint64 precision, and a real digital-record round trip.
Add changelog fragment for direct integer-sample comparisons.
tompollard
left a comment
There was a problem hiding this comment.
Thanks @Kulraj69, this is a clear fix with good tests. Comparing neighbouring samples instead of subtracting them avoids the overflow, and keeping floats on the old path keeps their behaviour with NaN unchanged.
I checked that the new tests fail without the fix (10 of 12) and pass with it, and that results are unchanged where the old code did not overflow: random small-integer and float signals, and record 100 in digital units, give the same peaks as on main.
One optional suggestion: on the integer path, tmp already holds -1, 0 or 1, so the three tmp[np.where(...)] = ... lines after it only matter for floats. Moving them into the else branch, or using tmp = np.sign(sig - tmp) there, would make that clearer. Happy to merge as it is.
## Problem `processing.find_local_peaks` sends integer signals through `scipy.ndimage.maximum_filter1d`, which rounds large 64-bit integers internally. Small differences on a large positive or negative offset can disappear, producing false or missing peaks. For example, an `int64` signal formed by adding `2**60` to `[0, 1, 0]` should retain its single peak at index 1. The same issue affects `uint64` values near `2**63` and negative `int64` offsets. ## Change Use the existing exact sample-by-sample scanning path when an integer signal contains values outside the exact integer range of a double. Ordinary integer and floating-point inputs retain their existing path. The constant-signal early return is unchanged. This deliberately trades speed for exact comparisons on those large-integer inputs; no performance improvement is claimed for them. It does not alter the vectorized common-input path introduced in #655 or the separate `find_peaks` overflow fix in #664. Add 18 regression cases spanning positive and negative `int64` offsets, `uint64`, two radii and three signal shapes, including a plateau. Expected peaks come from explicit bounded windows over the small, exact shape values. Four small-value dtype controls and input-immutability checks are included, plus a changelog fragment. ## Validation Python 3.12.16, isolated against upstream `98df2c9fba00943004ceac42677744f85a98e702`: - Unpatched regression file: 18 failed, 4 passed. - Exact files downloaded from the proposed branch, regression file plus `tests/test_processing.py` and `tests/test_hr.py`, excluding network tests: 238 passed. - Pinned Ruff 0.17.3 check and format passed for both changed Python files. - Strict Sphinx 9.0.4 HTML build (`-W --keep-going -E`) passed. Other private fixes were not included. Full-repository tests, performance benchmarks, other Python/dependency versions and remote CI were not run for this change. Prepared and tested with AI assistance; no independent human review is claimed.
Problem
find_peakssubtracts adjacent samples before classifying slope signs. For signed integer arrays, that subtraction can overflow and drop hard or plateau peaks. For unsigned integer arrays, assigning-1back into the unsigned temporary raisesOverflowErrorwith NumPy 2.This is reachable with digital samples returned by
rdrecord(..., physical=False, return_res=16), not only synthetic arrays.Change
For integer input only, compare adjacent samples directly and store the resulting slope signs in
int8. This avoids subtraction overflow without convertinguint64samples to floating point. The existing floating-point subtraction path is unchanged.Add regression cases for signed and unsigned limits, plateaus, closely spaced
uint64values, and awrsamp/ digitalrdrecordround trip. The round-trip fixture uses valid format-16 samples, not its missing-value sentinel. Add a changelog fragment.Validation
Local Python 3.12.16 / NumPy 2.5.5:
pytest tests/test_processing.py -k 'find_peaks or find_local_peaks' -p no:cacheprovider -q, 86 passed and 48 deselected.The full suite, documentation build, a second NumPy version, and upstream CI have not been run locally. This is not a clinical-validation claim.
AI assistance was used for the implementation, regression tests, and this description. The reported checks were executed locally; no independent human review is claimed.